<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 13:15
 */

namespace app\admin\controller;


use app\reuse\controller\ResponseJson;
use app\service\RatioService;
use think\Db;

class Ratio extends Common
{
    use ResponseJson;
    function index()
    {
        if(input("get.action")!='ajax'){
            $ratio=new RatioService();
            $todayRatio=$ratio->getsTodayRatio();
            $this->assign("todayRatio",floatval($todayRatio)*100);
            return $this->fetch();
        }else{
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 15;
            }
            $list=Db::table("gold_ratio")->order("apply_time desc")->paginate($listRows, false, ['query' => request()->param()]);
            return $this->jsonSuccessData($list);
        }
    }

    /**
     * @return array|mixed
     * @context 金币汇率添加
     */
    function ratio_add()
    {
        if(input("get.action")!='ajax'){
            return $this->fetch();
        }else{
            $param=input("post.");
            $param['create_time']=date("Y-m-d H:i:s");
            if(!empty($param['ratio']) and !empty($param["apply_time"])){
                Db::table("gold_ratio")->insert($param);
                $ratioService=new RatioService();
                $ratioService->delTodayRatio();
                return array("status"=>true);
            }else{
                return array("status"=>false,"msg"=>"请填写完整信息");
            }
        }
    }

    /**
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @context 金币比例修改
     */
    function ratio_write()
    {
        if(input("get.action")!='ajax'){
            $id=input("get.id");
            $info=Db::table("gold_ratio")->where(array("id"=>$id))->find();
            $this->assign("info",$info);
            return $this->fetch();
        }else{
            $param=input("post.");
            if(!empty($param['ratio']) and !empty($param["apply_time"])){
                Db::table("gold_ratio")->where(array("id"=>$param['id']))->update(array("ratio"=>$param['ratio'],
                    "apply_time"=>$param['apply_time'],"create_time"=>date("Y-m-d H:i:s")));
                $ratioService=new RatioService();
                $ratioService->delTodayRatio();
                return array("status"=>true);
            }else{
                return array("status"=>false,"msg"=>"请填写完整信息");
            }
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 金币比例删除
     */
    function ratio_del()
    {
        $id=input("post.id");
        Db::table("gold_ratio")->where(array("id"=>$id))->delete();
        $ratioService=new RatioService();
        $ratioService->delTodayRatio();
        return array("status"=>true);
    }

}